package com.ruoyi.controller.inventory;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.inventory.MesInventory;
import com.ruoyi.service.inventory.IMesInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库存信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/inventory/inventory")
public class MesInventoryController extends BaseController
{
    @Autowired
    private IMesInventoryService IMesInventoryService;

    /**
     * 查询库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesInventory mesInventory)
    {
        startPage();
        List<MesInventory> list = IMesInventoryService.selectMesInventoryList(mesInventory);
        return getDataTable(list);
    }

    /**
     * 导出库存信息列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:export')")
    @Log(title = "库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesInventory mesInventory)
    {
        List<MesInventory> list = IMesInventoryService.selectMesInventoryList(mesInventory);
        ExcelUtil<MesInventory> util = new ExcelUtil<MesInventory>(MesInventory.class);
        util.exportExcel(response, list, "库存信息数据");
    }

    /**
     * 获取库存信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:query')")
    @GetMapping(value = "/{inventoryId}")
    public AjaxResult getInfo(@PathVariable("inventoryId") Long inventoryId)
    {
        return success(IMesInventoryService.selectMesInventoryByInventoryId(inventoryId));
    }

    /**
     * 新增库存信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:add')")
    @Log(title = "库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesInventory mesInventory)
    {
        return toAjax(IMesInventoryService.insertMesInventory(mesInventory));
    }

    /**
     * 修改库存信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:edit')")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesInventory mesInventory)
    {
        return toAjax(IMesInventoryService.updateMesInventory(mesInventory));
    }

    /**
     * 删除库存信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:inventory:remove')")
    @Log(title = "库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inventoryIds}")
    public AjaxResult remove(@PathVariable Long[] inventoryIds)
    {
        return toAjax(IMesInventoryService.deleteMesInventoryByInventoryIds(inventoryIds));
    }

    /**
     * 根据仓库ID和物料ID查询库存
     */
    @GetMapping("/getByWarehouseAndMaterial")
    public AjaxResult getByWarehouseAndMaterial(Long warehouseId, Long materialId, String batchCode)
    {
        MesInventory inventory = IMesInventoryService.selectInventoryByWarehouseAndMaterial(warehouseId, materialId, batchCode);
        return success(inventory);
    }

    /**
     * 根据物料编码查询库存列表
     */
    @GetMapping("/listByMaterialCode/{materialCode}")
    public AjaxResult listByMaterialCode(@PathVariable String materialCode)
    {
        List<MesInventory> list = IMesInventoryService.selectInventoryByMaterialCode(materialCode);
        return success(list);
    }

    /**
     * 根据仓库编码查询库存列表
     */
    @GetMapping("/listByWarehouseCode/{warehouseCode}")
    public AjaxResult listByWarehouseCode(@PathVariable String warehouseCode)
    {
        List<MesInventory> list = IMesInventoryService.selectInventoryByWarehouseCode(warehouseCode);
        return success(list);
    }
}